<template>
    <div class="home">
        <home-poem></home-poem>
        <home-list ref="dutyList" v-show="dutyList.length"></home-list>
        <home-empty v-if="!dutyList.length"></home-empty>
    </div>
</template>

<script>
import axios from 'axios'

import HomePoem from './component/Poem'
import HomeList from './component/List'
import HomeEmpty from './component/Empty'

import util from '@/util/util'


export default {
    name: "Home",
    components: {
        HomePoem,
        HomeList,
        HomeEmpty
    },
    data () {
        return {
            dutyList: []
        }
    },
    methods: {
        getMonth (month) {
            if (month < 1) return month + 12;
            if (month > 12) return month - 12;
            return month;
        },
        getYear (year, month) {
            if (month < 1) return year - 1;
            if (month > 12) return year + 1;
            return year;
        },
        getMonthDayNum (year, month) {
            if (month < 1) {
                year --;
                month = 12;
            }
            if (month > 13) {
                year ++;
                month = 1;
            }
            switch(month) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        return 31;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        return 30;
                    case 2:
                        return year % 4 === 0 ? 29 : 28;
            }
        },
        getPageFirstDay (year, month) {
           
            //获取该月一号是周几
            var year = this.getYear(year, month);
            var month = this.getMonth(month);
            
            var date = new Date(year, month - 1, 1);
            var weekDay = date.getDay();
            var lastMonthYear = this.getYear(year, month - 1);
            var lastMonth = this.getMonth(month - 1);
            var lastMonthFirstDay = this.getMonthDayNum(lastMonthYear, lastMonth) - weekDay + 1;
            
            return lastMonthYear + "," + lastMonth + "," + lastMonthFirstDay + ";";
            
        },
        reqCalendar () {
            var datas = "";
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var _this = this;

            for (let i = 0; i < 5; i++) {
                datas += this.getPageFirstDay(year, month + i);
            }
            
            axios({
                method: 'get',
                url: '/getCalendar',
                params: {
                    dates: datas,
                    onePageDayNum: 42
                }
            })
            .then(function (response) {
                var datas = response.data;
                if (datas.status !== 0) return false;
                window.valueBus.dateDatas = datas.dates;
                
            }, function (err) {
                console.log(err);
            });

        },
        updateDutyList (datas) {
            for (let i = 0, len = datas.length; i < len; i++)
            {
                let date = new Date(parseInt(datas[i].warnTime));
                datas[i].clock = date.getHours() + ":" + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
                datas[i].halfDay = date.getHours() > 12 ? "下午" : "上午";
                datas[i].isMessageWarn = datas[i].isOpenMessageWarn === 1 ? true : false;
            }
            this.dutyList = datas;
            this.$refs.dutyList.updateList(datas);
        },
       reqDutyList () {
           var _this = this;
        
           
            let gazi_token = localStorage.getItem('gazi_token');
            axios({
                method: 'get',
                url: 'getDuty',
                params: {
                    gazi_token: gazi_token
                }
            })
            .then(function (response) {
                
                let datas = response.data;
                if (datas.status !== 0) return false;
                window.valueBus.duty = datas.data;
                _this.updateDutyList(datas.data);
            }, function (err) {

            });
           
           
       }
    },
    mounted () {

        var _this = this;
        //验证成功预先加载日历信息，
        util.checkLogin().then( function () {
            if (!window.valueBus.dateDatas)
            {
                _this.reqCalendar();
            }

            if (!window.valueBus.duty || window.valueBus.updateDuty)
            {
                _this.reqDutyList();
            }
            else
            {
                _this.updateDutyList(window.valueBus.duty);
            }
            
        }, function () {
        //验证失败重置界面到登录界面
            _this.$router.replace({
                path: '/login'
            })


        });
        
        
    },
    computed: {
        showDuty () {
            return this.dutyList.length > 0 ? false : true;
        }
    }
}
</script>

<style lang="stylus" scoped>
    .home
        width 100%
        height 100%
        position absolute
        top 0
        display flex
        flex-direction column
        align-items center
</style>


